from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from common.Utils import BlogDriver
from tests.BlogLogin import BlogLoginDriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC

# 测试简墨博客编辑页
class BlogEdit:
    url = ""
    driver = ""
    wait = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_edit.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()
        self.wait = WebDriverWait(self.driver, 5)
    # 登录状态下的简墨博客编辑的测试用例
    def EditByLogin(self):
        # 等待页面加载出来
        self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#editor > div.editormd-toolbar > div > ul > li:nth-child(21) > a > i")))
        # 输入简墨博客文章标题和内容并发布
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("第一篇文章-测试")
        # 是个插件，所以编辑区域不可操作，可以定位到菜单栏输入
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.editormd-toolbar > div > ul > li:nth-child(21) > a > i").click()
        # 屏幕截图记录
        BlogDriver.getScreeShot()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 查找最后一篇文章标题是自动化测试创建的
        self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(6) > div.title")))
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(6) > div.title").text
        BlogDriver.getScreeShot()
        assert text == "第一篇文章-测试"
        print("EditByLogin:>"+text)
    # 未登录状态下的简墨博客编辑的测试用例
    def EditByNotLogin(self):
        # 等待页面加载出来
        self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#editor > div.editormd-toolbar > div > ul > li:nth-child(21) > a > i")))
        # 输入简墨博客文章标题和内容并发布
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("第一篇文章-测试")
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.editormd-toolbar > div > ul > li:nth-child(21) > a > i").click()
        # 屏幕截图记录
        BlogDriver.getScreeShot()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 会自动跳转回简墨登录页
        sleep(0.5)
        current_url = self.driver.current_url
        assert current_url != self.url
        print("EditByNotLogin-before_url："+self.url)
        print("EditByNotLogin-after_url："+current_url)
        # 屏幕截图记录
        BlogDriver.getScreeShot()

BlogEditDriver = BlogEdit()
# BlogEditDriver.EditByNotLogin()
# BlogLoginDriver.LoginSucTest()
# BlogLoginDriver.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)").click()
# BlogEditDriver.EditByLogin()
# BlogDriver.driverQuit()